<%@page import="com.liferay.portal.kernel.bean.BeanPropertiesUtil"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.WindowState"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="com.liferay.portal.kernel.util.GetterUtil"%>
<%@page import="com.liferay.portal.kernel.util.StringUtil"%>
<%@page import="com.liferay.portal.kernel.util.CalendarFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.bean.BeanParamUtil"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="fr.smile.liferay.portlet.news.model.impl.NewsEntryImpl"%>
<%@page import="fr.smile.liferay.portlet.news.model.NewsEntry"%>
<%@page import="com.liferay.portal.kernel.util.UnicodeFormatter"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.HtmlUtil"%>
<%@page import="com.liferay.portal.kernel.language.LanguageUtil"%>
<%@page import="com.liferay.portal.service.UserGroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.UserGroup"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.service.RoleLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Role"%>
<%@page import="com.liferay.portal.service.OrganizationLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Organization"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Group"%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<portlet:defineObjects />

<%

WindowState windowState = null;

if (renderRequest != null) {
	windowState = renderRequest.getWindowState();
}
else if (resourceRequest != null) {
	windowState = resourceRequest.getWindowState();
}

String redirect = ParamUtil.getString(request, "redirect");

NewsEntry entry = (NewsEntry)request.getAttribute("NewsEntry");

long entryId = BeanParamUtil.getLong(entry, request, "entryId");

Calendar displayDate = CalendarFactoryUtil.getCalendar(timeZone, locale);

if (entry != null) {
	if (entry.getDisplayDate() != null) {
		displayDate.setTime(entry.getDisplayDate());
	}
}

Calendar expirationDate = CalendarFactoryUtil.getCalendar(timeZone, locale);
expirationDate.add(Calendar.MONTH, 1);
if (entry != null) {
	if (entry.getExpirationDate() != null) {
		expirationDate.setTime(entry.getExpirationDate());
	}
}

String content = BeanParamUtil.getString(entry, request, "content");
%>

<aui:form method="post"  enctype="multipart/form-data" name="fm" onSubmit='<%= "event.preventDefault(); " + renderResponse.getNamespace() + "saveEntry();" %>'>
	<aui:input name="cmd" type="hidden" />
	<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
	<aui:input name="entryId" type="hidden" value="<%= entryId %>" />
	<aui:input name="userId" type="hidden" value="<%= user.getUserId() %>" />

	<liferay-ui:header
		backURL="<%= redirect %>"
		title="label.newsletter"
	/>

	<%--liferay-ui:error exception="<%= EntryContentException.class %>" message="please-enter-valid-content" />
	<liferay-ui:error exception="<%= EntryDisplayDateException.class %>" message="please-enter-a-valid-display-date" />
	<liferay-ui:error exception="<%= EntryExpirationDateException.class %>" message="please-enter-a-valid-expiration-date" />
	<liferay-ui:error exception="<%= EntryTitleException.class %>" message="please-enter-a-valid-title" />
	<liferay-ui:error exception="<%= EntryURLException.class %>" message="please-enter-a-valid-url" / --%>

	<aui:model-context bean="<%= entry %>" model="<%= NewsEntry.class %>" />

	<aui:fieldset>
		<c:choose>
			<c:when test="<%= entry != null %>">

				<%
				boolean showScopeName = true;
				%>

				<%@ include file="/html/portlet/newslist/entry_scope.jspf" %>

			</c:when>
			<c:otherwise>
				<%
				String distributionScope = ParamUtil.getString(request, "distributionScope");

				long classNameId = -1;
				long classPK = -1;

				String[] distributionScopeArray = StringUtil.split(distributionScope);

				if (distributionScopeArray.length == 2) {
					classNameId = GetterUtil.getLong(distributionScopeArray[0]);
					classPK = GetterUtil.getLong(distributionScopeArray[1]);
				}

				boolean submitOnChange = false;
				%>

				<%@ include file="/html/portlet/newslist/entry_select_scope.jspf" %>

			</c:otherwise>
		</c:choose>

		<aui:input name="title" />
		
		<aui:input name="description"/>

		<aui:field-wrapper label="content">
			<liferay-ui:input-editor width="100%" />
			<aui:input name="content" type="hidden" />
		</aui:field-wrapper>
		
		<aui:input name="displayDate" value="<%= displayDate %>" />

		<aui:input name="expirationDate" value="<%= expirationDate %>" />
	</aui:fieldset>

	<aui:button-row>
		<aui:button type="submit" onClick='<%= renderResponse.getNamespace() + "extractCodeFromEditor();"%>'/>

		<aui:button onClick='<%= renderResponse.getNamespace() + "previewEntry();" %>' type="button" value="preview" />

		<aui:button onClick="<%= redirect %>" type="cancel" />
	</aui:button-row>
</aui:form>

<aui:script>

	function <portlet:namespace />initEditor() {
		var x = '<%= UnicodeFormatter.toString(content.replaceAll("'", "\\\\\'")) %>';
		return x;
	}
	function <portlet:namespace />previewEntry() {
		//document.<portlet:namespace />fm.action = '<portlet:actionURL><portlet:param name="struts_action" value="/announcements/preview_entry" /></portlet:actionURL>';
		document.<portlet:namespace />fm.target = '_blank';
		document.<portlet:namespace />fm.<portlet:namespace />cmd.value = "preview";
		document.<portlet:namespace />fm.submit();
	}

	function <portlet:namespace />saveEntry() {
		document.<portlet:namespace />fm.action = '<portlet:actionURL />';
		document.<portlet:namespace />fm.target = '';
		document.<portlet:namespace />fm.<portlet:namespace />cmd.value = '<%= (entry == null) ? "add" : "update"%>';
		submitForm(document.<portlet:namespace />fm);
	}
	
	function <portlet:namespace />extractCodeFromEditor() {
            var x = document.<portlet:namespace />fm.<portlet:namespace />content.value = window.<portlet:namespace />editor.getHTML();
	}

	<c:if test="<%= windowState.equals(WindowState.MAXIMIZED) %>">
		Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace />title);
	</c:if>
</aui:script>







<%--form  name="<portlet:namespace />fm" action="${editAction}" method="post" enctype="multipart/form-data"> --%>	
	